import Network from './network';
import kkui from 'kkui/kkui';

const apiCfg = WebCfg.api;
const baseURL = apiCfg['url'] + ':' + apiCfg['port'] + '/webapi';
const network = new Network(baseURL, kkui);

const api = {
  dictList: '/dict/dict_class',
  dict: {
    upload: '/dict/importTypes',
    getTab: '/dict/dict_types'
  },
  docList: '/apiArchives/index',
  upload: '/upload/uploadFile/files',
  delDoc: '/apiArchives/delete',

  editDoc: '/apiArchives/edit',
  addDoc: '/apiArchives/add',

  roadList: '/apiSection/index',
  addRoad: '/apiSection/add',
  editRoad: '/apiSection/edit',
  delRoad: '/apiSection/delete',

  devList:'/bizeventinfo/getEventList',
  importDevTab:'/bizeventinfo/importEventList',
  delDevData:'/bizeventinfo/deleteEventList',

  downloadTemplate: '/dict/downloadTemplate',

  downloadDoc:'/dict/downloadFj',

  exportCamereErrorTab: '/bizeventinfo/exportEventInfo',

  endTimer:'/task/stopCron',
  startTimer:'/task/startCron',

  smogData:'/index/wmIndex'
};



const server = {
  // 获取字典列表
  ServerGetDictList() {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.dictList,
          method: 'post'
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  ServerDictHandle(data, type) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.dict[type] + (type === 'upload' ? '?type=' + data.type : ''),
          method: 'post',
          data: type === 'upload' ? data.file : data,
          isFormData: type === 'upload'
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  // 获取电子档案列表
  ServerGetDocList(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.docList,
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  ServerUpload(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.upload,
          method: 'post',
          isFormData: true,
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  ServerAddDoc(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.addDoc,
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  ServerEditDoc(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.editDoc,
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },


  ServerDelDoc(id) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.delDoc + '/' + id,
          method: 'post'
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  // 获取道路列表
  ServerGetRoadList(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.roadList,
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  // 新增道路
  ServerAddRoad(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.addRoad,
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  // 编辑道路
  ServerEditRoad(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.editRoad,
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },


  // 删除道路
  ServerDelRoad(id) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.delRoad + '/' + id,
          method: 'post'
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  ServerDevList(data, type) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.devList,
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  // 上传用户表格
  ServerImportDevTab(data) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.importDevTab,
          method: 'post',
          data,
          isFormData: true
        },
        rst => {
          kkui.loading.hide(idx);
          if (rst.code === 0) {
            kkui.msg({cont: '操作成功！'});
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  ServerDelDevTab(data) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.delDevData,
          method: 'post',
          data
        },
        rst => {
          kkui.loading.hide(idx);
          if (rst.code === 0) {
            kkui.msg({cont: '操作成功！'});
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  ServerAddDevTab(data) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: '/bizeventinfo/savaEventList',
          method: 'post',
          data
        },
        rst => {
          kkui.loading.hide(idx);
          if (rst.code === 0) {
            kkui.msg({cont: '操作成功！'});
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  ServerUpdataDevTab(data) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: '/bizeventinfo/updateEventList',
          method: 'post',
          data
        },
        rst => {
          kkui.loading.hide(idx);
          if (rst.code === 0) {
            kkui.msg({cont: '操作成功！'});
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
// 下载模板
  ServerDownloadTmeplate(name) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.downloadTemplate,
          method: 'post',
          data: {
            tempalteName: name
          }
        },
        rst => {
          kkui.loading.hide(idx);
          if (rst.code === 0) {
            resolve(baseURL + '/' + rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  ServerDownloadDoc(name) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.downloadDoc,
          method: 'post',
          data: {
            fileName: name
          }
        },
        rst => {
          kkui.loading.hide(idx);
          if (rst.code === 0) {
            resolve(baseURL + '/' + rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  // 下载摄像机工况表
  ServerDownloadCameraErrTab(data) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.exportCamereErrorTab,
          method: 'post',
          data,
          responseType: 'blob'
        },
        rst => {
          resolve(rst);
        }
      )
    })
  },

  // 开启定时器
  ServerStartTimer() {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.startTimer,
          method: 'post',
        },
        rst => {
          resolve(rst);
        }
      )
    })
  },

  ServerEndTimer() {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.endTimer,
          method: 'post',
        },
        rst => {
          resolve(rst);
        }
      )
    })
  },
  //
  ServerGetSmogData() {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: api.smogData,
          method: 'post',
        },
        rst => {
          resolve(rst.data && rst.data['wmList']);
        }
      )
    })
  },

   //获取相机排序列表
  async ServerCameraSortList(data){
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: "/camera/sortList",
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

   //保存变更后的排序
   ServerSaveCameraSort(data){
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: "/camera/saveSortList",
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

   //获取预设位列表
   ServerCameraYswList(data){
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: "/cameraYsw/list",
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  //获取预设点详情
  ServerCameraYswById(data){
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: "/cameraYsw/getYswData",
          method: 'post',
          data
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  //新增预置点
  ServerAddCameraYsw(data) {
    const idx = kkui.loading.show();
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: '/cameraYsw/add',
          method: 'post',
          data
        },
        rst => {
          kkui.loading.hide(idx);
          if (rst.code === 0) {
            kkui.msg({cont: '操作成功！'});
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },
  // 删除预置点
  ServerDelYswById(id) {
    return new Promise(function (resolve, reject) {
      network.ajax({
          url: '/cameraYsw/delete/' + id,
          method: 'post'
        },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({title: '出错啦！', cont: '错误信息：' + (rst.msg || '')});
            reject(rst.msg);
          }
        }
      )
    })
  },

  //获取摄像机算法列表
  ServerCameraAiById(data) {
    return new Promise(function (resolve, reject) {
      network.ajax({
        url: "/cameraAi/list",
        method: 'post',
        data
      },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({ title: '出错啦！', cont: '错误信息：' + (rst.msg || '') });
            reject(rst.msg);
          }
        }
      )
    })
  },

  //update摄像机算法列表
  ServerUpdateCameraAi(data) {
    console.log(data);
    return new Promise(function (resolve, reject) {
      network.ajax({
        url: "/cameraAi/update",
        method: 'post',
        data
      },
        rst => {
          if (rst.code === 0) {
            resolve(rst['data']);
          } else {
            kkui.alert({ title: '出错啦！', cont: '错误信息：' + (rst.msg || '') });
            reject(rst.msg);
          }
        }
      )
    })
  },

    //批量update摄像机算法列表
    ServerUpdateCameraAiList(data) {
      console.log(data);
      return new Promise(function (resolve, reject) {
        network.ajax({
          url: "/cameraAi/updateCameraAiList",
          method: 'post',
          data
        },
          rst => {
            if (rst.code === 0) {
              resolve(rst['data']);
            } else {
              kkui.alert({ title: '出错啦！', cont: '错误信息：' + (rst.msg || '') });
              reject(rst.msg);
            }
          }
        )
      })
    },
};



export default server;
